import sqlite3


def init_db():
    conn = sqlite3.connect('./license_plates.db')
    c = conn.cursor()
    # 创建 current_plates 表
    c.execute('''CREATE TABLE IF NOT EXISTS current_plates(
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                plate_number TEXT UNIQUE NOT NULL, 
                enter_time DATETIME NOT NULL, 
                filename TEXT NOT NULL, 
                location INTEGER NOT NULL, 
                create_time DATETIME DEFAULT CURRENT_TIMESTAMP
            );''')

    c.execute('''CREATE TABLE IF NOT EXISTS plates
                 (id INTEGER PRIMARY KEY AUTOINCREMENT,
                  plate_number TEXT, /* 车牌号 */
                  enter_time DATETIME, /* 进入停车场时间 */
                  exit_time DATETIME, /* 离开停车场时间 */
                  filename TEXT, /* 输入图片文件名 */
                  exit_filename TEXT, 
                  location INTEGER NOT NULL,
                  parking_seconds INTEGER, 
                  total_fee REAL, 
                  create_time DATETIME DEFAULT CURRENT_TIMESTAMP)''')  # 修改停车位置 location 为 INTEGER 类型

    # 创建 abnormal_plates 表
    c.execute('''CREATE TABLE IF NOT EXISTS abnormal_plates (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                plate_number TEXT UNIQUE NOT NULL, 
                enter_time DATETIME NOT NULL, 
                filename TEXT NOT NULL, 
                location INTEGER NOT NULL, 
                create_time DATETIME DEFAULT CURRENT_TIMESTAMP
            );''')

    # 创建 fee_settings 表
    c.execute('''CREATE TABLE IF NOT EXISTS fee_settings (
                 id INTEGER PRIMARY KEY AUTOINCREMENT,
                 fee_within_1_hour REAL,
                 fee_after_1_hour REAL,
                 fee_within_24_hours REAL
             );''')

    # 创建 users 表
    c.execute('''CREATE TABLE IF NOT EXISTS admin (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                username TEXT UNIQUE NOT NULL,
                password TEXT NOT NULL,
                created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            );''')

    # 创建 users 表
    c.execute('''CREATE TABLE IF NOT EXISTS user (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                username TEXT UNIQUE NOT NULL,
                password TEXT NOT NULL,
                created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            );''')

    conn.commit()
    conn.close()